import routes from 'server/route/index';
import utils from 'server/core/utils';
import playerStorage from 'js/core/playerStorage';
import eventHandle from 'server/handle/eventHandle';
import playerHandle from 'server/handle/playerHandle';
import MazeInfo from 'server/model/mazeInfo';
import ExamineHandle from 'server/handle/examineHandle';
import battleHandle from 'server/handle/battleHandle';
import packageHandle from 'server/handle/packageHandle';
import equipmentHandle from 'server/handle/equipmentHandle';
import {
    totalConfig,
    propName
} from 'server/config/templateConfig.js';
let buildingConfig = totalConfig.building;
let mapConfig = totalConfig.map;
let mazeConfig = totalConfig.maze;
let cardsConfig = totalConfig.card;
let itemsConfig = totalConfig.item;
// 1: '拾取', 2: '采集', 3: '采矿', 4: '采伐', 5: '钓鱼', 6: '战斗', 7: '战斗', 8: '战斗', 9: '调查', 10: '对话', 11路口,12增益(恢复,buff)

//拾取物品
routes.setLogin('cardPickUp', [
    ['index', 'number'],
    ['itemId', 'number']
], (player, msg, cb) => {
    let index = msg.index,
        itemId = msg.itemId;
    let mazeInfos = player.mazeInfos;
    let mazeType = mazeInfos.type;
    let currentCards = mazeType === 1 ? mazeInfos.monsterCards : mazeInfos.resourceCards;
    let currentCard = currentCards[index];
    if (!currentCard) {
        cb(1);
        return;
    }

    if (!currentCard.info || !currentCard.info.items) {
        cb(2);
        return;
    }
    let items = currentCard.info.items;
    let itemIndex = items.findIndex(element => element[0] === itemId);
    if (itemIndex === -1) {
        cb(3);
        return;
    }
    if (packageHandle.isHasEmpty(player.bag, 1) === -1) {
        cb(4);
        return;
    }
    let num = 1;
    if (parseInt(itemId / 10000) === 51) { //装备
        packageHandle.addItemComponent(player.bag, items[itemIndex][2]);
    } else {
        num = items[itemIndex][1];
        packageHandle.addItem(player.bag, itemId, num);
    }

    items.splice(itemIndex, 1);

    cb(0, {
        log: '获得了' + propName[itemId] + 'x' + num
    })
}, {
    1: '卡牌没有不存在',
    2: '不是拾取类型',
    3: '物品不存在',
    4: '背包空间不足'
});


// //采集,采伐,采矿,钓鱼
// routes.setLogin('cardCollection', [
//     ['index', 'number'],
//     ['itemId', 'number']
// ], (player, msg, cb) => {
//     let index = msg.index,
//         itemId = msg.itemId;
//     let mazeInfos = player.mazeInfos;
//     let mazeId = mazeInfos.mazeId;
//     let zoneId = mazeInfos.zoneId;
//     let currentCard = mazeInfos.currentCards[index];
//     if (!currentCard || currentCard.type !== 1) {
//         cb(1);
//         return;
//     }
//     let cardId = currentCard.id;
//     let cardConfigInfo = cardsConfig[cardId];
//     let cardType = cardConfigInfo.type
//     if (cardType !== 2 && cardType !== 3 && cardType !== 4) {
//         cb(2);
//         return;
//     }
//     if (!cardConfigInfo.items[itemId]) {
//         cb(3);
//         return;
//     }
//     if (currentCard.items[itemId] <= 0) {
//         cb(4);
//         return;
//     }

//     let tool = null,
//         toolText;
//     switch (cardType) {
//         case 2:
//             tool = player.bag.collectionItem;
//             toolText = 'collectionItem';
//             break;
//         case 3:
//             tool = player.bag.cutItem;
//             toolText = 'cutItem';
//             break;
//         case 4:
//             tool = player.bag.miningItem;
//             toolText = 'miningItem';
//             break;
//         default:
//             break;
//     }
//     if (!tool) {
//         cb(5);
//         return
//     }
//     if (currentCard.hp <= 0) {
//         cb(6);
//         return
//     }
//     let toolId = tool.templateId;
//     let toolConfigInfo = itemsConfig[toolId];
//     if (tool.hp * toolConfigInfo.hurt >= currentCard.hp) {
//         tool.hp -= Math.ceil(currentCard.hp / toolConfigInfo.hurt);
//         let item = [itemId, currentCard.items[itemId]];
//         currentCard.info = {
//             items: [item]
//         }
//         currentCard.hp = cardsConfig[cardId].maxHp;
//     } else {
//         currentCard.hp -= tool.hp * toolConfigInfo.hurt;
//         delete player.bag[toolText];
//     }
//     cb(0)
// }, {
//     1: '卡牌没有不存在',
//     2: '不是采集类型',
//     3: '物品不存在',
//     4: '物品数量不足',
//     5: '没有工具',
// });


//离开
routes.setLogin('cardLeave', [
    ['index', 'number']
], (player, msg, cb) => {
    let index = msg.index;
    let mazeInfos = player.mazeInfos;
    let currentCard = mazeInfos.currentCards[index];
    if (!currentCard) {
        cb(1)
        return;
    }

    if (mazeInfos.cards.length > 0) {
        mazeInfos.currentCards[index] = utils.getOneFromArrayAndRemove(mazeInfos.cards);
    } else {
        mazeInfos.currentCards[index] = {
            indexId: currentCard.indexId,
            type: 0
        };
    }

    cb(0, {
        log: ''
    })
}, {
    1: '卡牌不存在'
});

//岔路口
routes.setLogin('goZone', [
    ['index', 'number'],
    ['number', 'number']
], (player, msg, cb) => {
    let index = msg.index;
    let number = msg.number;
    let mazeInfos = player.mazeInfos;
    let card = mazeInfos.currentCards[index];
    if (!card) {
        cb(1)
        return;
    }
    let cardId = card.id;
    if (cardsConfig[cardId].type !== 11) {
        cb(2);
        return;
    }
    let zoneId = cardsConfig[cardId].zoneIds[number];
    if (!zoneId) {
        cb(3)
    }
    let mazeId = mazeInfos.mazeId;
    mazeInfos.zoneId = zoneId;
    MazeInfo.mazeReset(mazeInfos);
    cb(0);
}, {
    1: '卡牌不存在',
    2: '不是通道',
    3: 'index不存在'
});

//卡牌调查
routes.setLogin('cardExamine', [
    ['index', 'number']
], (player, msg, cb) => {
    let index = msg.index;
    let mazeInfos = player.mazeInfos;
    let currentCard = mazeInfos.currentCards[index];
    if (!currentCard) {
        cb(1);
        return;
    }
    let cardId = currentCard.id;
    if (cardsConfig[cardId].type !== 9) {
        cb(2);
        return;
    }
    let info = ExamineHandle.do(cardId);
    currentCard.info = info;
    cb(0, {
        info: info,
        log: ''
    })
});

//卡牌使用
routes.setLogin('cardUse', [
    ['index', 'number']
], (player, msg, cb) => {
    let index = msg.index;
    let mazeInfos = player.mazeInfos;
    let currentCard = mazeInfos.currentCards[index];
    if (!currentCard || currentCard.type !== 1) {
        cb(1);
        return;
    }
    let cardId = currentCard.id;
    if (cardsConfig[cardId].type !== 12) {
        cb(2);
        return;
    }

    let cardInfo = mazeInfos.mazeData[cardId];
    cardInfo.use = true;
    cb(0, {
        log: ''
    })
});


// 迷宫前进
routes.setLogin('mazeForward', null, (player, msg, cb) => {
    let mazeInfos = player.mazeInfos;

    cb(0, {
        log: ''
    })
});

// 迷宫重置
routes.setLogin('mazeReset', null, (player, msg, cb) => {
    let mazeInfos = player.mazeInfos;
    MazeInfo.mazeReset(mazeInfos);
    cb(0, {
        log: ''
    })
});

// 迎战
routes.setLogin('cardBattle', [
    ['index', 'number']
], (player, msg, cb) => {
    let index = msg.index;
    let mazeInfos = player.mazeInfos;
    let currentCards = mazeInfos.monsterCards;
    let currentCard = currentCards[index];
    if (!currentCard || currentCard.type !== 2) {
        cb(1);
        return;
    }
    // let battleMsg = battleHandle.battle(player, currentCard.id);
    let battleMsg = {
        result: 1
    };
    if (battleMsg.result === 1) {
        let equipment = equipmentHandle.create(1);
        currentCard.info = {
            items: [
                [equipment.templateId, 1, equipment]
            ]
        }
    }
    player.basicInfo.adventureState = 2;
    currentCards[index]
    cb(0, {
        battleMsg: battleMsg
    })
});

// 战斗测试
routes.setLogin('testBattle', null, (player, msg, cb) => {
    let battleMsg = battleHandle.test(player)
    cb(0, {
        battleMsg: battleMsg
    })
});

// 对话 
//npc对话1获得任务-完成任务获得东西或者情报2给予东西获得情报
routes.setLogin('cardDialogue', [
    ['index', 'number']
], (player, msg, cb) => {
    let index = msg.index;
    let mazeInfos = player.mazeInfos;
    let currentCard = mazeInfos.currentCards[index];
    if (!currentCard || currentCard.type !== 1) {
        cb(1);
        return;
    }
    let cardId = currentCard.id;
    if (cardsConfig[cardId].type !== 10) {
        cb(2);
        return;
    }

    let mazeId = mazeInfos.mazeId;
    let zoneId = mazeInfos.zoneId;
    let cardInfo = mazeInfos.mazeData[cardId];
    cardInfo.trigger = true;
    cb(0, {
        log: ''
    })
});